from helper_diskSpaceManager import * #Helper_diskSpaceManager
from helper_getDataFromBlock import *
from helper_fileChopper import *
from helper_saveToDisk import *
from File import *
from Directory import *


class FileSystem(object):
    
    def __init__(self,aDisk):

        self.dirInodes = {} 
        self.freeInodes =[]  
        self.filesInodes = {} 
        
        self.hardDisk = aDisk
        
        self.h_spaceMan = Helper_diskSpaceManager(aDisk) 
        self.h_fileChopper = Helper_fileChopper()
        self.h_saveToDisk = Helper_saveToDisk(aDisk)
        self.h_getDataFromBlock = Helper_getDataFromBlock()
        
        self.root = self.mkRoot()
        
#############    
#AUXILIARES
#############
                 
    def hayFreeInodes(self):
        return self.h_spaceMan.hayFreeInodes()
        
    def getDataBlockSize(self):
        return 8      
    
    
    def startUpFreeInodes(self):
        self.freeDirInodes = self.hardDisk.getDirInodeKeys()

#############    
#ARCHIVOS
#############
    
    def saveFile(self, aFile, aDirectory):
        
        blocksNeeded = self.h_fileChopper.blocksNeeded(aFile, self.getDataBlockSize())
        
        if ((self.hayFreeInodes()) & (self.h_spaceMan.hayNFreeBlocks(blocksNeeded))):
            self.saveFileAux(aFile, aDirectory, blocksNeeded)
                  
    def saveFileAux(self, aFile, aDirectory,cantBlocks):
        
        listOfParts = self.h_fileChopper.fileChopp(aFile, self.getDataBlockSize())
        listOfFreeBlocks = self.h_spaceMan.pickNFreeBlocks(cantBlocks)
        
        self.h_saveToDisk.persist(listOfParts, listOfFreeBlocks)
        
        
        aDirectory.addFile(aFile)
        
        elId = self.h_spaceMan.pickFreeInode()
        elInode = self.hardDisk.getInode(elId)     
        elInode.setInode(aFile.getName(),listOfFreeBlocks)
        self.filesInodes[aFile.getName()] = elId
        
                
    def openFile(self,aFileName):
        
        elInodeId = self.filesInodes[aFileName] 
        elInode = self.hardDisk.getInode(elInodeId)
        dataBlockUsedList = elInode.getBlocks()
        
        aPartsList = self.h_getDataFromBlock.getDataFromBlocks(self.hardDisk,dataBlockUsedList)
        
        dataForFile = self.h_fileChopper.fileJoiner(aPartsList)
        
        niuFile = Filez()
        niuFile.setName(aFileName)
        niuFile.setLocation(elInode)
        niuFile.setData(dataForFile)
        
        return niuFile

#############  
#DIRECTORIO  
#############

    def delTree(self,aDir):
        del(aDir) 
        

    def getDirectory(self, aDirectory):
        return self.dirIndodes[aDirectory]
        
    def mkDir(self, aDirectoryName, dirFather):
        
        if (self.hayFreeInodes()):
            dir = DDirectory(aDirectoryName)
            
            dirFather.addSon(dir)
            
            inode = self.h_spaceMan.pickFreeInode()
            self.filesInodes[inode] = dir
            
        return dir
    
    
    def mkRoot(self):
        dir = DDirectory("root")
        dir.setRoot = True
        return  dir
    
    
